import 'package:auto_route/auto_route.dart';
import 'package:sonora/models/sj_login_response_data.dart';
import 'package:sonora/utils/sj_storage_utils.dart';
import 'package:sonora/views/navigation/routes/sj_app_router.dart';

class SjAuthGuard extends AutoRouteGuard {
  final bool isLogin;
  final bool isFirst;

  SjAuthGuard({required this.isLogin, required this.isFirst});

  @override
  void onNavigation(NavigationResolver resolver, StackRouter router) async {
    SjLoginData? loginData = await SjStorageUtils.getLoginInfo();
    bool? logout = await SjStorageUtils.getAccountLogout();
    if (loginData != null && loginData.userInfo != null) {
      resolver.next(true);
    } else if (isFirst) {
      router.replace(const SjOnboardingRoute());
    } else if (!isLogin || (logout != null && logout == true)) {
      router.replace(const SjLoginRoute());
    } else {
      resolver.next(true);
    }
  }
}